package cn.schoolwow.workflow.module.common.flow;

import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowInstanceHistory;

public class AddWorkFlowHistoryFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        Long workFlowInstanceId = flowContext.getData("workFlowInstanceId", Long.class);
        if(null==workFlowInstanceId){
            WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
            workFlowInstanceId = workFlowInstance.getId();
        }
        String assigner = flowContext.getData("assigner", String.class);
        String message = flowContext.checkData("message", String.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("添加工作流实例历史记录");
        WorkFlowInstanceHistory workFlowInstanceHistory = new WorkFlowInstanceHistory();
        workFlowInstanceHistory.setWorkFlowInstanceId(workFlowInstanceId);
        workFlowInstanceHistory.setAssigner(assigner);
        workFlowInstanceHistory.setMessage(message);
        transaction.insert(workFlowInstanceHistory);
    }

    @Override
    public String name() {
        return "添加工作流操作历史记录";
    }

}
